"""角色信息获取"""

from src.core import log
from src.db import dbm, dbs
from src.core.variables.status import *


def roles_list(schema):
    """角色列表"""
    with dbm.get("sys_default") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        result = {}
        db_result = db_session.query(dbs.sys_default.Role).all()
        for i in db_result:
            result[i.name] = {
                "uid": i.uid,
                "name": i.name,
                "description": i.description,
                "status": i.status,
                "create_time": i.create_time,
                "update_time": i.update_time,
                "create_user": i.create_user,
                "update_user": i.update_user,
            }
        return SUCCESS_CODE_CUSTOMIZE(result, 200)


def role_info(schema):
    """单个角色信息"""
    with dbm.get("sys_default") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = (
            db_session.query(dbs.sys_default.Role)
            .filter(dbs.sys_default.Role.uid == schema.uid)
            .first()
        )
        if db_result:
            result = {
                "uid": db_result.uid,
                "name": db_result.name,
                "description": db_result.description,
                "status": db_result.status,
                "create_time": db_result.create_time,
                "update_time": db_result.update_time,
                "create_user": db_result.create_user,
                "update_user": db_result.update_user,
            }
            return SUCCESS_CODE_CUSTOMIZE(result, 200)
        return ERROR_CODE_CUSTOMIZE("角色不存在", 404)
